﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WineStore.DAL;
using PagedList.Mvc;
using PagedList;
using WineStore.Models;
using System.Data;
using System.Data.Entity;
using System.IO;

namespace Project_AnPhat.Controllers
{

    public class CategoryNewsController : Controller
    {

        // GET: /CategoryNews/ShowCategoryNews/
        public ViewResult ShowCategoryNews(int? page)
        {
            using (var db = new WineDbContext())
            {
                var categoryNews = db.CategoryNews.OrderByDescending(c => c.CategoryNewsId);

                int pageSize = 10;
                int pageNumber = (page ?? 1);

                // Thiet lap thuoc tinh khi phan trang
                PagedListRenderOptions pro = new PagedListRenderOptions();
                pro.MaximumPageNumbersToDisplay = 5;
                pro.LinkToLastPageFormat = "Last";
                pro.DisplayLinkToLastPage = true;
                pro.LinkToFirstPageFormat = "First";
                pro.DisplayLinkToFirstPage = true;
                pro.LinkToNextPageFormat = ">>";
                pro.DisplayLinkToNextPage = true;
                pro.LinkToPreviousPageFormat = "<<";
                pro.DisplayLinkToPreviousPage = true;

                ViewBag.Pro = pro;

                return View(categoryNews.ToPagedList(pageNumber, pageSize));
            }
        }

        // GET: /CategoryNews/CreateCategoryNews/
        [HttpGet]
        public ActionResult CreateCategoryNews()
        {
            return View();
        }

        // POST: /CategoryNews/CreateCategoryNews/
        [HttpPost]
        public ActionResult CreateCategoryNews(CategoryNews categorynews, HttpPostedFileBase CategoryNewsImage)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    // Luu anh len server
                    string path = System.IO.Path.Combine(Server.MapPath("~/Content/Images/CategoryNews"), System.IO.Path.GetFileName(CategoryNewsImage.FileName));
                    CategoryNewsImage.SaveAs(path);
                    // Luu anh vao database 
                    categorynews.CategoryNewsImage = CategoryNewsImage.FileName;
                    using (var db = new WineDbContext())
                    {
                        db.Entry(categorynews).State = EntityState.Added;
                        db.SaveChanges();
                        return RedirectToAction("ShowCategoryNews", "CategoryNews");
                    }
                }
            }
            catch (DataException)
            {
                ModelState.AddModelError("", "Unable to save changes");
            }
            return View(categorynews);
        }

        // GET: /CategoryNews/EditCategoryNews/
        [HttpGet]
        public ActionResult EditCategoryNews(int id = 0)
        {
            CategoryNews categorynews = null;
            using (var db = new WineDbContext())
            {
                categorynews = db.CategoryNews.Find(id);
                if (categorynews == null)
                {
                    return HttpNotFound();
                }
            }
            return View(categorynews);
        }

        // POST: /CategoryNews/EditCategoryNews/
        [HttpPost]
        public ActionResult EditCategoryNews(CategoryNews categorynews, HttpPostedFileBase CategoryNewsImage)
        {
            try
            {
                // Lay ve duong dan anh cu
                categorynews.CategoryNewsImage = (new WineDbContext().CategoryNews.Find(categorynews.CategoryNewsId)).CategoryNewsImage;
                if (ModelState.IsValidField("CategoryNewsName_VN") && ModelState.IsValidField("CategoryNewsName_EN"))
                {
                    // Neu update anh moi thi thuc hien khoi lenh trong than cau lenh if
                    if (CategoryNewsImage != null)
                    {
                        // Xoa anh cu tren server
                        FileInfo fileInfo = new FileInfo(Server.MapPath("~/Content/Images/CategoryNews/" + categorynews.CategoryNewsImage));
                        if (fileInfo != null)
                        {
                            fileInfo.Delete();
                        }
                        // Luu anh moi len server
                        string path = System.IO.Path.Combine(Server.MapPath("~/Content/Images/CategoryNews"), System.IO.Path.GetFileName(CategoryNewsImage.FileName));
                        CategoryNewsImage.SaveAs(path);
                        // Luu duong dan anh vao database 
                        categorynews.CategoryNewsImage = CategoryNewsImage.FileName;
                    }

                    using (var db = new WineDbContext())
                    {
                        db.Entry(categorynews).State = EntityState.Modified;
                        db.SaveChanges();
                        return RedirectToAction("ShowCategoryNews", "CategoryNews");
                    }
                }
            }
            catch (DataException)
            {
                ModelState.AddModelError("", "Unable to save changes");
            }
            return View(categorynews);
        }

        // GET: /CategoryNews/DeleteCategoryNews/
        public ActionResult DeleteCategoryNews(int id)
        {
            CategoryNews categorynews = null;
            using (var db = new WineDbContext())
            {
                categorynews = db.CategoryNews.Find(id);

                if (categorynews == null)
                {
                    return HttpNotFound();
                }
            }
            return View(categorynews);
        }

        // POST: /CategoryNews/DeleteCategoryNews/
        [HttpPost, ActionName("DeleteCategoryNews")]
        public ActionResult DeleteConfirmed(int id)
        {
            CategoryNews categorynews = null;
            try
            {
                using (var db = new WineDbContext())
                {
                    categorynews = db.CategoryNews.Find(id);
                    db.Entry(categorynews).State = EntityState.Deleted;
                    db.SaveChanges();
                    return RedirectToAction("ShowCategoryNews", "CategoryNews");
                }
            }
            catch (DataException)
            {
                ModelState.AddModelError("", "Unable to save changes");
            }
            return View(categorynews);
        }
    }
}

